// Package prom is a helper package to get a prom compatible middleware.
package prom

import (
	"context"

	"github.com/gogf/gf/net/ghttp"

	"github.com/slok/go-http-metrics/middleware"
)

// Handler returns a prom measuring middleware.
func Handler(handlerID string, m middleware.Middleware) ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		rp := &reporter{r: r}
		m.Measure(handlerID, rp, func() {
			r.Middleware.Next()
		})
	}

}

type reporter struct {
	r *ghttp.Request
}

func (r *reporter) Method() string { return r.r.Request.Method }

func (r *reporter) Context() context.Context { return r.r.Request.Context() }

func (r *reporter) URLPath() string { return r.r.Request.URL.Path }

func (r *reporter) StatusCode() int { return r.r.Response.Status }

func (r *reporter) BytesWritten() int64 { return int64(r.r.Response.BufferLength()) }
